// 验证工具函数

/**
 * 验证邮箱格式
 */
const isValidEmail = (email) => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  return emailRegex.test(email)
}

/**
 * 验证密码强度
 */
const isValidPassword = (password) => {
  // 至少6位，包含字母和数字
  const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/
  return passwordRegex.test(password)
}

/**
 * 验证用户名格式
 */
const isValidUsername = (username) => {
  // 3-20位，只能包含字母、数字、下划线
  const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/
  return usernameRegex.test(username)
}

/**
 * 验证文章标题
 */
const isValidArticleTitle = (title) => {
  return title && title.trim().length >= 5 && title.trim().length <= 100
}

/**
 * 验证文章内容
 */
const isValidArticleContent = (content) => {
  return content && content.trim().length >= 10
}

/**
 * 验证文件类型
 */
const isValidFileType = (file, allowedTypes) => {
  return allowedTypes.includes(file.mimetype)
}

/**
 * 验证文件大小
 */
const isValidFileSize = (file, maxSize) => {
  return file.size <= maxSize
}

/**
 * 清理输入字符串
 */
const sanitizeInput = (input) => {
  if (typeof input !== 'string') return input
  return input.trim().replace(/[<>]/g, '')
}

/**
 * 验证分页参数
 */
const validatePagination = (page, limit) => {
  const pageNum = parseInt(page) || 1
  const limitNum = parseInt(limit) || 10
  
  return {
    page: Math.max(1, pageNum),
    limit: Math.min(100, Math.max(1, limitNum))
  }
}

export {
  isValidEmail,
  isValidPassword,
  isValidUsername,
  isValidArticleTitle,
  isValidArticleContent,
  isValidFileType,
  isValidFileSize,
  sanitizeInput,
  validatePagination
} 